/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package view;

import client.ClientView;
import java.sql.SQLException;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import model.Flight;

/**
 * Creates a view to register a user.
 *
 * @author Tobias Robinson
 */
public class RegisterAirportView extends JDialog implements ActionListener {
    
    private String name = null;
    private String tax = null;

    //components of the register window
    private JTextField nameField = new JTextField(10);
    private JTextField taxField = new JTextField(10);
    private JLabel nameLabel = new JLabel("Enter name:  ");
    private JLabel taxLabel = new JLabel("Enter tax:  ");
    private JButton createButton = new JButton("Register");

    /**
     * Creates a view to register user.
     *
     * @param parent tells view to run over main window
     */
    public RegisterAirportView(JFrame parent) throws SQLException {
        super(parent, "Register Flight", true);
        setResizable(true);

        //content pane for login window
        JPanel registerPane = new JPanel();
        setContentPane(registerPane);

        //user GridBag layout
        GridBagLayout gb = new GridBagLayout();
        GridBagConstraints c = new GridBagConstraints();
        registerPane.setLayout(gb);
        registerPane.setBorder(BorderFactory.createEmptyBorder(12, 12, 11, 11));

        // place the namelabel
        c.gridwidth = GridBagConstraints.RELATIVE;
        c.insets = new Insets(10, 0, 10, 0);
        c.anchor = GridBagConstraints.WEST;
        gb.setConstraints(nameLabel, c);
        registerPane.add(nameLabel);

        //add origin combo box to panel
        c.gridwidth = GridBagConstraints.REMAINDER;
        c.insets = new Insets(10, 0, 5, 10);
        c.anchor = GridBagConstraints.EAST;
        gb.setConstraints(nameField, c);
        registerPane.add(nameField);

        // place destination label
        c.gridwidth = GridBagConstraints.RELATIVE;
        c.insets = new Insets(10, 0, 10, 0);
        c.anchor = GridBagConstraints.WEST;
        gb.setConstraints(taxLabel, c);
        registerPane.add(taxLabel);

        //add destination combo box to panel
        c.gridwidth = GridBagConstraints.REMAINDER;
        c.insets = new Insets(10, 0, 5, 10);
        c.anchor = GridBagConstraints.EAST;
        gb.setConstraints(taxField, c);
        registerPane.add(taxField);

        // place the create button
        c.gridwidth = GridBagConstraints.REMAINDER;
        c.insets = new Insets(10, 10, 5, 10);
        c.anchor = GridBagConstraints.CENTER;
        gb.setConstraints(createButton, c);
        registerPane.add(createButton);

        // end of layout

        // Register password field and OK button with action event handler.
        // An action event is generated when the return key is pressed while
        // the cursor is in the password field or when the OK button is pressed.
        createButton.addActionListener(this);
    }

    /**
     * When user click button or presses enter processes information.
     *
     * @param e event type
     */
    public void actionPerformed(ActionEvent e) {
            name = new String(nameField.getText());
            tax = new String(taxField.getText());

            Flight.addAirport(name, tax);
            ClientView.printMessage("Airport: " + name + " created with tax: " + tax + "\n");
            dispose();
    }
}
